import 'package:hrf_app/common/extension/ex_visibility.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class TextInputMultipleEdit extends StatelessWidget {
  /// 文字字符串
  final String? text;

  final Widget? textWidget;
  final bool isCustomText;

  /// 标题
  final String labelText;

  /// 自定义参数 在回调时会原封不动的返回去
  final dynamic customParam;

  /// 是否显示编辑按钮  (editParm && isShowEdit)
  final bool isShowEdit;

  ///编辑文字
  final String editText;

  final Widget? editWidget;
  final bool isCustomEdit;

  /// 第一个时customParam 的内容
  final String? Function(dynamic)? editCallBack;

  /// 编辑的权限串 (editParm && isShowEdit)
  final String editParm;

//是否显示必填写 *
  final bool isMustBeEnter;

  /// 字体大小
  final double size;

  /// padding
  final EdgeInsets? linePadding;
  final BoxDecoration? boxDecoration;

  //  构造函数
  const TextInputMultipleEdit(
      {Key? key,
      required this.labelText,
      this.customParam = "",
      this.text,
      this.isMustBeEnter = false,
      this.size = 16,
      this.isShowEdit = false,
      this.editText = "编辑",
      this.editCallBack,
      this.editWidget,
      this.textWidget,
      this.boxDecoration = const BoxDecoration(color: Colors.white),
      this.linePadding = const EdgeInsets.all(8),
      this.editParm = "",
      this.isCustomText = false,
      this.isCustomEdit = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
          decoration: boxDecoration, padding: linePadding, child: _buildLeft()),
      Container(
          decoration: boxDecoration,
          padding: linePadding,
          child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [_buildRight()]))
    ]);
  }

  Widget _buildLeft() {
    String mustBeEnter = isMustBeEnter ? "*" : " ";

    Widget editCustomWidget = Text(
      editText,
      style: TextStyle(
        fontSize: size - 2,
        color: Colors.blue,
      ),
    );
    if (isCustomEdit) {
      editCustomWidget = editWidget!;
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            Container(
                margin: EdgeInsets.only(top: 4),
                child: Text(
                  mustBeEnter,
                  style: TextStyle(color: Colors.red, fontSize: size - 2),
                )),
            Text(
              labelText,
              style: TextStyle(fontSize: size, color: Colors.black54),
            ),
          ],
        ),
        GestureDetector(
            onTap: () {
              editCallBack!(customParam);
            },
            behavior: HitTestBehavior.opaque,
            excludeFromSemantics: false,
            dragStartBehavior: DragStartBehavior.start,
            child: editCustomWidget.hasPermAndVisible(
                hasPerm: editParm, visible: isShowEdit)),
      ],
    );
  }

  Widget _buildRight() {
    Widget textCustomWidget = Text(
      text ?? "暂无",
      style: TextStyle(
        fontSize: size,
      ),
    );
    if (isCustomText) {
      textCustomWidget = textWidget!;
    }
    return Expanded(
        child: Container(
            alignment: Alignment.centerRight, child: textCustomWidget));
  }
}
